package com.zhicheng.controller;

import com.zhicheng.pojo.Category;
import com.zhicheng.pojo.CategoryTwo;
import com.zhicheng.pojo.dto.CategoryPageQueryDTO;
import com.zhicheng.pojo.result.PageResult;
import com.zhicheng.pojo.result.Result;
import com.zhicheng.pojo.vo.CategoryTwoVO;
import com.zhicheng.pojo.vo.CategoryVO;
import com.zhicheng.pojo.vo.GoodVO;
import com.zhicheng.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/category")
@Api(tags = "分类相关接口")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 根据id查询一级分类
     * @param id
     * @return
     */
    @GetMapping
    @ApiOperation("根据id查询一级分类")
    public Result<CategoryVO> getCategoryById(Long id){
        CategoryVO categoryVO = categoryService.getCategoryById(id);
        return Result.success(categoryVO);
    }

    /**
     * 根据二级分类id查询商品
     * @param id
     * @return
     */
    @GetMapping("/sub")
    @ApiOperation("根据二级分类id查询商品")
    public Result<CategoryTwoVO> getGoodBySubCategoryId(Long id){
        CategoryTwoVO goods = categoryService.getGoodBySubCategoryId(id);
        return Result.success(goods);
    }

    /**
     * 根据id进行分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @PostMapping("/goods/temporary")
    @ApiOperation("根据id进行分页查询")
    public Result<PageResult<GoodVO>> list(@RequestBody CategoryPageQueryDTO categoryPageQueryDTO){
        PageResult<GoodVO> pageResult = categoryService.list(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    @GetMapping("/search")
    @ApiOperation("根据商品名称进行分页查询")
    public Result<PageResult<GoodVO>> getByGoodName(String name,int page,int pageSize){
        PageResult<GoodVO> pageResult = categoryService.getByGoodName(name,page,pageSize);
        return Result.success(pageResult);
    }

    @GetMapping("/sub/list")
    @ApiOperation("查询二级分类")
    public Result<List<CategoryTwo>> getSubCategory(){
        List<CategoryTwo> categoryTwos = categoryService.getSubCategory();
        return Result.success(categoryTwos);
    }

    @GetMapping("/alllist")
    @ApiOperation("查询所有一级分类")
    public Result<List<Category>> getCategory(){
        List<Category> category = categoryService.getCategory();
        return Result.success(category);
    }

    @GetMapping("/list")
    @ApiOperation("分页查询一级分类")
    public Result<PageResult<Category>> getCategoryPage(int page,int pageSize){
        PageResult<Category> pageResult = categoryService.getCategoryPage(page,pageSize);
        return Result.success(pageResult);
    }
    @PostMapping("/add")
    @ApiOperation("添加一级分类信息")
    private Result addCategory(@RequestBody Category category) {
        categoryService.addCategory(category);
        return Result.success();
    }

    @PutMapping("/update")
    @ApiOperation("修改一级分类信息")
    private Result updateCategory(@RequestBody Category category) {
        categoryService.updateCategory(category);
        return Result.success();
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除一级分类信息")
    private Result deleteCategory(Long id) {
        categoryService.deleteCategory(id);
        return Result.success();
    }

    @GetMapping("/sub/pagelist")
    @ApiOperation("分页查询二级分类")
    public Result<PageResult<CategoryTwo>> getSubCategoryPage(Long categoryId,int page,int pageSize){
        PageResult<CategoryTwo> pageResult = categoryService.getSubCategoryPage(categoryId,page,pageSize);
        return Result.success(pageResult);
    }

    @PostMapping("/sub/add")
    @ApiOperation("添加二级分类信息")
    private Result addSubCategory(@RequestBody CategoryTwo categoryTwo) {
        categoryService.addSubCategory(categoryTwo);
        return Result.success();
    }

    @PutMapping("/sub/update")
    @ApiOperation("修改二级分类信息")
    private Result updateSubCategory(@RequestBody CategoryTwo categoryTwo) {
        categoryService.updateSubCategory(categoryTwo);
        return Result.success();
    }

    @DeleteMapping("/sub/delete")
    @ApiOperation("删除二级分类信息")
    private Result deleteSubCategory(Long id) {
        categoryService.deleteSubCategory(id);
        return Result.success();
    }
}
